package com.jiangyg.mall.coupon.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyg.mall.core.enums.Enable;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.utils.bean.BeanUtils;
import com.jiangyg.mall.coupon.constant.ColumnConstant;
import com.jiangyg.mall.coupon.dao.SeckillPromotionDao;
import com.jiangyg.mall.coupon.dto.SeckillPromotionDTO;
import com.jiangyg.mall.coupon.entity.SeckillPromotion;
import com.jiangyg.mall.coupon.vo.SeckillPromotionVO;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class SeckillPromotionServiceImpl extends BaseServiceImpl<SeckillPromotionDao, SeckillPromotion> implements SeckillPromotionService {

    @Autowired
    public SeckillPromotionServiceImpl(SeckillPromotionDao seckillPromotionDao) {
        super(seckillPromotionDao);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Pagination<SeckillPromotionVO> selectPagination(Pagination<SeckillPromotionVO> pagination) throws Exception {
        return this.executeSelectPagination(pagination, SeckillPromotionVO.class);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<SeckillPromotionVO> selectValidListWithin3Days() throws Exception {
        // 当天 0 点
        final LocalDate now = LocalDate.now();
        final LocalDateTime startTime = LocalDateTime.of(now, LocalTime.MIN);
        // 后天 23点
        final LocalDate lastDate = now.plusDays(2);
        final LocalDateTime endTime = LocalDateTime.of(lastDate, LocalTime.MAX);
        // 查询活动列表（开始时间在3天内的活动列表）
        final QueryWrapper<SeckillPromotion> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnConstant.STATUS, Enable.Y.icode);
        wrapper.between(ColumnConstant.START_TIME, startTime, lastDate);
        final List<SeckillPromotion> dataList = this.baseMapper.selectList(wrapper);
        return BeanUtils.transformList(dataList, SeckillPromotionVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(SeckillPromotionDTO dto) throws Exception {
        return this.executeSaveOrUpdate(dto) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public SeckillPromotionVO selectById(Long seckillPromotionId) throws Exception {
        return this.executeSelectById(seckillPromotionId, SeckillPromotionVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> seckillPromotionIds) {
        return this.baseMapper.deleteBatchIds(seckillPromotionIds) > 0;
    }

}
